<?php
/**
 * Created by PhpStorm.
 * User: PanChaoZhi
 * Date: 2017/9/19
 * Time: 15:14
 *
 * PHP中的算法问题
 */

/**
 * 阶乘算法(n!)
 *
 * @param int $num , 进行阶乘的数值
 * @return int
 */
function factorial($num)
{
    $num = (int)$num;

    if ($num < 2)
        return 1;

    $res = $num;
    for ($i = $num; $i > 1; $i--)
        $res *= ($i - 1);

    return $res;
}

//echo factorial(5);


/**
 * 高斯算法 1+2+3+4+5+...+n
 *
 * @param $n
 * @return float|int
 */
function gauss($n)
{
    $n = (int)$n;

    return (1 + $n) * $n / 2;
}

//echo gauss(5);


